import type { AnyFunction } from './src/types';

export interface ContextOptions {
  /** 错误处理方式 */
  errorFunction?: AnyFunction<any>;
  /** 获取 token 的方法 */
  getTokenFunction?: () => unknown;
  /** 没有权限的方法 */
  unauthorizedFunction?: (msg?: string) => void;
  /** 公共的 api 请求 url */
  apiUrl?: string;
  uploadUrl?: string;
  responseParams?: {
    code: string,
    message: string
    data: string
  }
}

export let context: Partial<ContextOptions> = {
  getTokenFunction: () => {},
  unauthorizedFunction: () => {},
  errorFunction: (err: {message: string;}) => {
    console.log('接口请求出错了', err, err.message);
  },
  responseParams: {
    code: 'code',
    message: 'message',
    data: 'data'
  }
};

export const initRequest = async(_context: Partial<ContextOptions>) => {
  context = { ...context, ..._context };
};
